package com.ruoyi.web.controller.inv;

import com.ruoyi.buy.domain.entity.PO.InvInventoryInfo;
import com.ruoyi.buy.domain.entity.buy0001.BuySpaImportData;
import com.ruoyi.buy.domain.entity.inv0002.Inv0002ImportData;
import com.ruoyi.buy.domain.entity.inv0002.Inv0002TableData;
import com.ruoyi.buy.domain.entity.inv0002.Inv0002ViewData;
import com.ruoyi.buy.service.IInvInventoryInfoService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 库存信息Controller
 * 
 * @author ruoyi
 * @date 2023-12-18
 */
@RestController
@RequestMapping("/inv/inv0002")
@Transactional
public class InvInventoryInfoController extends BaseController
{
    @Autowired
    private IInvInventoryInfoService invInventoryInfoService;

    /**
     * 查询库存信息列表
     */

    @GetMapping("/list")
    public TableDataInfo list(InvInventoryInfo invInventoryInfo)
    {
        startPage();
        List<Inv0002TableData> list = invInventoryInfoService.selectInvInventoryInfoList(invInventoryInfo);
        return getDataTable(list);
    }

    /**
     * 导出库存信息列表
     */
    @Log(title = "库存信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, InvInventoryInfo invInventoryInfo)
    {
        List<Inv0002TableData> list = invInventoryInfoService.selectInvInventoryInfoList(invInventoryInfo);
        ExcelUtil<Inv0002TableData> util = new ExcelUtil<Inv0002TableData>(Inv0002TableData.class);
        util.exportExcel(response, list, "库存信息数据");
    }

    /**
     * 获取库存信息详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(invInventoryInfoService.selectInvInventoryInfoById(id));
    }

    /**
     * 新增库存信息
     */
    @Log(title = "库存信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody InvInventoryInfo invInventoryInfo)
    {
        return toAjax(invInventoryInfoService.insertInvInventoryInfo(invInventoryInfo));
    }

    /**
     * 修改库存信息
     */
    @Log(title = "库存信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody InvInventoryInfo invInventoryInfo)
    {
        return toAjax(invInventoryInfoService.updateInvInventoryInfo(invInventoryInfo));
    }

    /**
     * 删除库存信息
     */
    @Log(title = "库存信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(invInventoryInfoService.deleteInvInventoryInfoByIds(ids));
    }

    @PostMapping("/getViewData")
    public AjaxResult getViewData(@RequestBody  InvInventoryInfo invInventoryInfo)
    {
        List<Inv0002ViewData> list = invInventoryInfoService.getViewData(invInventoryInfo);
        return success(list);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<Inv0002ImportData> util = new ExcelUtil<>(Inv0002ImportData.class);
        util.importTemplateExcel(response, "期初库存数据导入模版");
    }

    @Log(title = "期初数据导入", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        ExcelUtil<Inv0002ImportData> util = new ExcelUtil<>(Inv0002ImportData.class);
        List<Inv0002ImportData> list = util.importExcel(file.getInputStream());
        this.invInventoryInfoService.importData(list);
        return success(1);
    }


}
